import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbactivitysignup } from '../../../atoms/db';

const logger = anylogger('pages/api/activityregistration.api');

export type IData = {
	/**
	* id
	*/
	id: string;
	/**
	 * 用户名称
	 */
	name: string
	/**
	 * 报名状态:0取消1正常
	 */
	state: number;
	/**
	 * 备注
	 */
	remark: string;
};


export type Result = {
	count: number;
	data: IData[];
};

export type Query = {
	/**
	 * 活动id
	 */
	aid: string;
}

/**
 * 活动报名信息
 */
const handler = an48<Result>();

handler.get(async (req, res) => {
	try {
		logger.debug('msg query:', req.query);
		const { aid } = req.query as Query;
		//获取数据
		const d = await tbactivitysignup()
			.select<IData[]>({
				id: 'activitysignup.id',
				name: 'user.name',
				state: 'activitysignup.state',
				remark: 'activitysignup.remark'
			})
			.leftJoin('user', 'activitysignup.userid', 'user.userid')
			.where({
				aid
			})
			.andWhere('state', 1);
		//返回数据		
		res.status(200).json({ count: d.length, data: d });
	} catch (error) {
		logger.error(error);
		logger.trace(error);
		res.status(200).json({
			count: 0,
			data: []
		});
	}
});

export const config = {} as PageConfig;

export default handler;
